package cn.netjava.softkid.chapter13;
import java.sql.*;


public class StatementAndPreparedStatement {

	static Connection con;
	static 
	{
		try {
			Class.forName("com.mysql.jdbc.Driver");
			String url = "jdbc:mysql://localhost:3306/dip";
			con = DriverManager.getConnection(url, "root","123456");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	public static void query(String sql) throws SQLException
	{
		PreparedStatement stat = con.prepareStatement(sql);
		stat.setInt(1, 200801);
		stat.setInt(2,21);
		ResultSet rs = stat.executeQuery();
		ResultSetMetaData rsmd = rs.getMetaData();
		int rows = rsmd.getColumnCount();
		while(rs.next())
		{
			for(int i=1;i<=rows;i++)
			{
				System.out.print(rs.getString(i)+"\t");
			}
			System.out.println();
		}
		System.out.println(".........................");
		stat.setInt(1, 200802);
		stat.setInt(2, 20);
		rs = stat.executeQuery();
		rsmd = rs.getMetaData();
		rows = rsmd.getColumnCount();
		while(rs.next())
		{
			for(int i=1;i<=rows;i++)
			{
				System.out.print(rs.getString(i)+"\t");
			}
			System.out.println();
		}
	}
	
	
	public static void main(String[] args) throws SQLException 
	{
		String sql = "SELECT * FROM student WHERE id=? or age=?";
		query(sql);

	}

}
